package dao.impl;

import dao.MiddleDao;
import util.DBUtils;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public class MiddleDaoImpl extends DBUtils implements MiddleDao {
    @Override
    public int insertMiddle(int roleid, String[] ids) {
        String sql = "insert into middle(roleid,menuid) values(?,?)";
        int k = 0;
        pps = getPps(sql);
        try {
            for (String id : ids) {
                pps.setInt(1, roleid);
                pps.setString(2, id);
                pps.addBatch();
            }
            pps.executeBatch();
            k = 1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return k;
    }

    @Override
    public int deleteByRoleId(int roleid) {
        String sql = "delete from middle where roleid = ?";
        int update = 0;
        try {
            update = update(sql, roleid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }
}
